<?php
session_start();
if ( isset($_GET['down']) )
{
	unset($_SESSION['auth']);
	$down = true;
}
if ( isset($_SESSION['auth']) )
{
	header("Location: index.php");
	exit();
}
if ( isset($_POST['wl-ip']) )
{
	function valid_ip($ip) { return ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $ip)) ? FALSE : TRUE; }
	if ( !valid_ip( $_POST['wl-ip'] ) ) $_POST['wl-ip'] = gethostbyname( $_POST['wl-ip'] );
	$fp = @fsockopen( $_POST['wl-ip'], $_POST['wl-port'], $errno, $errstr, 2);
	if (!$fp) echo "OFF";
	else
	{
		fwrite($fp, "AUTH ".$_POST['wl-username']." ".$_POST['wl-password']);
		$ok = fread($fp, 1);
		if ( $ok == "1" )
		{
			fread($fp, 9);
			$session = fread($fp, 40);
			fwrite( $fp, $session. " GETPORT" );
			$port = fread( $fp, 4 );
			$_SESSION['auth'] = $session;
			$_SESSION['username'] = $_POST['wl-username'];
			$_SESSION['ip'] = $_POST['wl-ip'];
			$_SESSION['port'] = $port;
			$_SESSION['rport'] = $_POST['wl-port'];
			echo "OK";
		}
		else echo "WRONG";
		fclose($fp);
	}
	exit();
}
?>
<html>
    <head>
        <title>LU Rcon - Login</title>
		
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link type="text/css" rel="stylesheet" href="css/login_style.css" />
		<link media="screen" href="css/jquery.msg.css" rel="stylesheet" type="text/css"/>
		
		<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
		<script type="text/javascript" src="js/jquery.msg.min.js"></script>
		
		<script>
		function SubmitLogin()
		{
			$('#login-status').show();
			$('#login-status').slideDown( 300 );
			$("#login-status").html('<img src="img/loader.gif"> Logging in...');
			var dataString = 'wl-ip='+ $("input#wl-ip").val() + '&wl-port=' + $("input#wl-port").val() + '&wl-username=' + $("input#wl-username").val() + '&wl-password=' + $("input#wl-password").val();
			$.ajax({
				type: "POST",
				url: "login.php",
				data: dataString,
				success: function(data) {
					if ( data == "OK" ) {
						$("#login-status").html('Logging OK. Loading Interface...');
						setTimeout('LoadInterface()', 2000);
						$("#login-status").addClass('msg-ok');
					}
					else if ( data == "OFF" ) {
						$("#login-status").html('Error - Server refused connection. Please check your IP and Port.');
						$("#login-status").addClass('msg-error');
					}
					else if ( data == "WRONG" ) {
						$("#login-status").html('Error - Invalid Username/Password.');
						$("#login-status").addClass('msg-error');
					}
				}
			});
			return false;
		}
		function LoadInterface()
		{
			window.location = 'index.php';
		}
		<?php if ( isset( $down ) ) echo '
		$(window).load(function () {
			$("#login-status").html(\'Error - Server closed connection.\');
			$("#login-status").addClass(\'msg-error\');
			$(\'#login-status\').show();
			$("#wl-password").focus();
		});
		'; ?>
  		</script>
    </head>
    <body>
        <div id="login-container-outer" class="radius">
            <div id="login-container" class="radius">
                <div id="login-header" class="radius-top">
                        <img src="img/logo.png" alt="login logo" />
                </div>
                <div id="login-content">
                    <form action="" method="post" id="login" name="login" onsubmit="return SubmitLogin();">
                        <label for="wl-ip">IP</label>
                        <input type="text" id="wl-ip" name="wl-ip" <?php if ( ( isset( $down ) ) && ( isset($_SESSION['ip']) ) ) echo 'value="'.$_SESSION['ip'].'" '; ?>/>
                        <label for="wl-port">Rcon Port</label>
                        <input type="text" id="wl-port" name="wl-port" value="2332" />
						<label for="wl-username">Username</label>
                        <input type="text" id="wl-username" name="wl-username" <?php if ( ( isset( $down ) ) && ( isset($_SESSION['username']) ) ) echo 'value="'.$_SESSION['username'].'" '; ?>/>
                        <label for="wl-password">Password</label>
                        <input type="password" id="wl-password" name="wl-password" />
                        <input type="submit" value="Login" id="wl-btn" name="wl-btn" class="grey" />
                    </form>
                </div>
				<div id="login-status">eekhax</div>
                <div id="login-extra" class="radius-bottom">
                    <a class="afooter-link">&copy; Thijn 2011 - All rights reserved.</a>
                </div>
            </div>
        </div>
    </body>
</html>